[Core/Rust Server] Support multiple request body/response body types#19411
[Core/Rust Server] Support multiple request body/response body types#19411richardwhiuk wants to merge 9 commits into
Conversation
Support a request / response which can have multiple body content types
e.g.
requestBody:
content:
application/json:
schema:
type: string
application/xml:
schema:
type: string
responses:
200:
content:
application/json:
type: string
application/xml:
type: string
|
thanks for the PR. please resolve the merge conflicts when you've time cc @OpenAPITools/generator-core-team |
@wing328 I've sorted the merge conflicts - happy to merge, or do you want additional reviews? |
|
let me take another tomorrow and will merge if no question from me |
| @Getter @Setter | ||
| public String arrayModelType; | ||
| public boolean isAlias; // Is this effectively an alias of another simple type | ||
| public boolean isVariant; // Does this represent a schema variant? |
There was a problem hiding this comment.
can you please add a comment showing an example of a schema variant to put everyone on the same page?
| */ | ||
| @SuppressWarnings("static-method") | ||
| public String toVariantName(List<String> names) { | ||
| return "oneOf<" + String.join(",", names) + ">"; |
There was a problem hiding this comment.
i think this is for a schema covering all the schema specified in the responses (e.g. 2xx, 3xx, 4xx).
should this be anyOf instead as technically the payload in the response can technically match more than one schema defined in the responses?
| for (Map.Entry<String, ApiResponse> ar : op.getResponses().entrySet()) { | ||
| ApiResponse a = ModelUtils.getReferencedApiResponse(openAPI, ar.getValue()); | ||
| Map<String, Schema> responseSchemas = ModelUtils.getSchemasFromResponse(openAPI, a); | ||
| if (responseSchemas != null && responseSchemas.size() > 1 && supportsMultipleResponseTypes) { |
There was a problem hiding this comment.
what about also checking supportsMultipleResponseTypes at line 508?
| requestSchemas = ModelUtils.getSchemasFromRequestBody(b); | ||
| } | ||
| if (requestSchemas != null) { | ||
| if (requestSchemas.size() > 1 && supportsMultipleRequestTypes) { |
There was a problem hiding this comment.
what about combining the conditions in line 499 and 500 into one?
|
@richardwhiuk when you've time, can you please also PM me via Slack as I've few questions if you don't mind? |
Overview
Support a request / response which can have multiple body content types - e.g. in V3:
Details
Existing Changes
DefaultCodgen.fromResponsenow takes the Operation ID as a parameter.The following methods have been renamed for clarity:
ModelUtils:
getSchemaFromResponse->getFirstSchemaFromResponsegetSchemaFromContent->getFirstSchemaFromContentDefaultCodegen:
hasBodyParameter->hasFirstBodyParameterfromRequestBodyToFormParameters->fromFirstRequestBodyToFormParametersgetContentType->getFirstContentTypeEnabling Support
This is opt in for a generator - the generator needs to set
supportsMultipleRequestTypesandsupportsMultipleResponseTypesin order to opt into supporting multiple request and response types.This is required because per generator changes are required in order to support the new function, and a bunch of existing tests depend on the current behaviour, assuming the user only cares about the first request / response type.
Modelling of multiple request/responses
In order to reuse as much existing function as possible, and isolate the required changes, the mechanism used is as follows.
a) If support for multiple request and response types is disabled, no change in behaviour.
b) If the operation being generated doesn't have multiple requests, or multiple responses, there is no change in behaviour.
c) If the operation being generated has multiple requests, then a top level
CodgenParameterfor the body parameter is generated.This has a child
CodegenParameterfor each of the different Content Type for the request. Each child parameter hascontentTypeset.d) If the operation being generated has multiple responses, then a top level
CodegenResponseis generated.This has a child
CodegenResponsefor each of the different Content Type for the request. Each child response hascontentTypeset.In addition to the multiple
CodegenRequest/CodegenResponse, an additional set of models are generated. This allows an additional layer of indirection is done to flag which content type is used/will be used. The models in question are flagged withisVariantset to true, and alias the body parameter being used.Overview of Rust Server generation
Here is a request which either takes a JSON request, or an XML request
Here is a response, which either returns an XML Response or a JSON response
In this case, because the responses are defined the same (which is a restriction in OpenAPI V2, but not in OpenAPI V3), the definition for the responses is the same:
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming 7.6.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)@OpenAPITools/generator-core-team
Rust Technical Committee: @frol @farcaller @paladinzh @jacob-pro
Expect this to need plenty of review.